Completed
Push — master ( 9cd845...b1fdb8 )
by
unknown
02:45
created

duplicate.js ➔ duplicate   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
dl 0
loc 48
rs 9.125
nop 6

1 Function

Rating   Name   Duplication   Size   Complexity  
B duplicate.js ➔ ... ➔ ??? 0 44 6
1
import {
2
  abeExtend,
3
  Manager,
4
  config,
5
  cmsData,
6
  cmsOperations
7
} from '../../'
8
9
var duplicate = function(oldFilePath, template, newPath, name, req, isUpdate = false) {
10
  var p = new Promise((resolve, reject) => {
11
    abeExtend.hooks.instance.trigger('beforeDuplicate', oldFilePath, template, newPath, name, req, isUpdate)
12
13
    var json = {}
14
    var revisions = []
15
    if(oldFilePath != null) {
16
      var files = Manager.instance.getList()
17
      var fileWithoutExtension = oldFilePath.replace('.' + config.files.templates.extension, '.json')
18
19
      var doc = null
20
      Array.prototype.forEach.call(files, (file) => {
21
        if (file.path.indexOf(fileWithoutExtension) > -1) {
22
          doc = file
23
        }
24
      })
25
26
      if(doc.revisions != null) {
27
        revisions = doc.revisions
28
29
        if(revisions != null && revisions[0] != null) {
30
          json = cmsData.file.get(revisions[0].path)
31
        }
32
      }
33
      
34
      delete json.abe_meta
35
    }
36
37
    abeExtend.hooks.instance.trigger('afterDuplicate', json, oldFilePath, template, newPath, name, req, isUpdate)
38
39
    var pCreate = cmsOperations.create(template, newPath, name, req, json, (isUpdate) ? false : true)
40
    pCreate.then((resSave) => {
41
      if (isUpdate) {
42
        abeExtend.hooks.instance.trigger('beforeUpdate', json, oldFilePath, template, newPath, name, req, isUpdate)
43
        cmsOperations.remove.remove(oldFilePath)
44
      }
45
      resolve(resSave)
46
    },
47
    () => {
48
      reject()
49
    }).catch(function(e) {
50
      console.error('[ERROR] abe-duplicate.js', e)
51
      reject()
52
    })
53
  })
54
55
  return p
56
}
57
58
export default duplicate